package com.home.fansea.fliter;


import com.home.fansea.entity.User;
import com.home.fansea.service.TokenService;
import com.home.fansea.utils.UserHolder;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class RefreshInterceptor implements HandlerInterceptor {



    private RedisTemplate redisTemplate;

    public RefreshInterceptor(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Resource
    TokenService tokenService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        System.out.println("----------------token过滤器--------------------");
        //获取请求头中的token
        User user = tokenService.getLoginUser(request);
        //如果用户存在，刷新token有效期
        if (user != null) {
            //刷新token有效期
            tokenService.refreshToken(user);
            //将用户信息保存到shiro中
//            ShiroUtils.setUser(user);
            UserHolder.saveUser(user);
//            System.out.println("user = " + ShiroUtils.getUser());
            System.out.println("user = " + UserHolder.getUser());
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }


}
